package com.jml.service.order.impl;


import com.jml.entity.OrderEntity;
import com.jml.mapper.OrderMapper;
import com.jml.service.order.openfeign.DistributeServiceFeign;


import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;


@RestController
public class OrderServiceImpl {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DistributeServiceFeign distributeServiceFeign;

    @RequestMapping("/insertOrder")
    @GlobalTransactional
    public String insertOrder(int age) {
        String orderId = System.currentTimeMillis() + "";
        OrderEntity newOrder = createOrder(orderId);
        // 1.向订单数据库表插入数据
        int result = orderMapper.addOrder(newOrder);
        if (result < 0) {
            return "插入订单失败";
        }
        // 2.调用派单服务，实现对该笔订单派单 远程调用派单接口 HTTP协议
        String resultDistribute = distributeServiceFeign.distributeOrder(orderId);
        // 判断调用接口失败的代码...
        int j = 1 / age;
        return resultDistribute;
    }

    public OrderEntity createOrder(String orderId) {
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setName("order-name");
        orderEntity.setOrderCreatetime(new Date());
        // 价格是300元
        orderEntity.setOrderMoney(300d);
        // 状态为 未支付
        orderEntity.setOrderState(0);
        Long commodityId = 30L;
        // 商品id
        orderEntity.setCommodityId(commodityId);
        orderEntity.setOrderId(orderId);
        return orderEntity;
    }
}
